// pages/shopBag/shopBag.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    frameList: [],
    driveList: [],
    showList: [],
    topShow: true,
    listNum: -1,
    showImage:"",
    showImageShow:false,
    userInfo:''
  },
  frameCh() {
    this.setData({
      topShow: true,
      showList: this.data.frameList
    })
  },
  driveCh() {
    this.setData({
      topShow: false,
      showList: this.data.driveList
    })
  },
  listNumCh(data) {
    if(this.data.topShow){
      if(data.currentTarget.dataset.index==this.data.listNum){
        this.setData({
          listNum: -1,
          showImageShow:false
        })
      }else{
        this.setData({
          listNum: data.currentTarget.dataset.index,
          showImageShow:true,
          showImage:this.data.frameList[data.currentTarget.dataset.index].image
        })
      }
    }else{
      if(data.currentTarget.dataset.index==this.data.listNum){
        this.setData({
          listNum: -1,
        })
      }else{
        this.setData({
          listNum: data.currentTarget.dataset.index,
        })
      }
    }
  },
  playDress(data) {
    // console.log(data.currentTarget.dataset.index)
    if (this.data.showList[data.currentTarget.dataset.index].status) {
      let timestamp = (Date.parse(new Date()) / 1000).toString();
      let _token =
        "userId=" +
        app.globalData.user_id +
        "&type=2&token=" +
        app.globalData.userToken +
        "&time=" +
        timestamp +
        "&goodId=" +
        this.data.showList[data.currentTarget.dataset.index].goodsId +
        "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
      let params = {
        userId: app.globalData.user_id,
        token: app.globalData.userToken,
        time: timestamp,
        type: 2,
        goodId: this.data.showList[data.currentTarget.dataset.index].goodsId,
        sign: md5.hexMD5(_token),
      };
      axios.axios({
        url: "/goods/usegood",
        data: params
      }).then((res) => {
        console.log(res.data.result),
          wx.showToast({
            title: res.data.text,
            icon: 'none'
          })
        this.getshopBag()
      })
    } else {
      let timestamp = (Date.parse(new Date()) / 1000).toString();
      let _token =
        "userId=" +
        app.globalData.user_id +
        "&type=1&token=" +
        app.globalData.userToken +
        "&time=" +
        timestamp +
        "&goodId=" +
        this.data.showList[data.currentTarget.dataset.index].goodsId +
        "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
      let params = {
        userId: app.globalData.user_id,
        token: app.globalData.userToken,
        time: timestamp,
        type: 1,
        goodId: this.data.showList[data.currentTarget.dataset.index].goodsId,
        sign: md5.hexMD5(_token),
      };
      axios.axios({
        url: "/goods/usegood",
        data: params
      }).then((res) => {
        console.log(res.data.result),
          wx.showToast({
            title: res.data.text,
            icon: 'none'
          })
        this.getshopBag()
      })
    }
  },
  getshopBag() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/goods/goodsbag",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          showList: res.data.result.head,
          frameList: res.data.result.head,
          driveList: res.data.result.drive
        })
    })
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      // console.log(res.data.result),
        this.setData({
          userInfo:res.data.result.user
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getshopBag()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})